import config from '../config/config.js'
// const mongoose = require('mongoose');
import mongoose from 'mongoose'
const db = () => {
  //设置 strictQuery 为 true
  mongoose.set('strictQuery', true)

  //3. 连接 mongodb 服务                        数据库的名称
  const url = `mongodb://${config.user}:${config.password}@${config.DBHOST}:${config.DBPORT}/?authSource=${config.DBNAME}`
  console.log(url)
  mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })

  //4. 设置回调
  // 设置连接成功的回调  once 一次   事件回调函数只执行一次
  mongoose.connection.once('open', () => {
    console.log('数据库连接成功！')
  })

  // 设置连接错误的回调
  mongoose.connection.on('error', () => {
    console.log('数据库连接错误！')
  })

  //设置连接关闭的回调
  mongoose.connection.on('close', () => {
    console.log('连接关闭')
  })
}
export default db
